package com.fishing.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;


/**
 * 获取客户端的基本信息
 * */
public class getRequestInfromation {
	/**
	 * 各种代理
	 *
	 * X-Forwarded-For：Squid服务代理
	 * Proxy-Client-IP：apache服务代理
	 * WL-Proxy-Client-IP：weblogic服务代理
	 * X-Real-IP：nginx服务代理
	 * HTTP_CLIENT_IP：有些代理服务器
	 */
	
	public static final String LOCAL_IP = "127.0.0.1";//本地ip地址
	public static final String DEFAULT_IP = "0:0:0:0:0:0:0:1";//默认ip地址
	public static final int DEFAULT_IP_LENGTH = 15;//默认ip地址长度
    public static String getIpAddress(HttpServletRequest request) {
    	String ip = request.getHeader("x-forwarded-for");//squid 服务代理
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");//apache服务代理
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");//weblogic 代理
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");//有些代理
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP"); //nginx代理
        }

        /*
        * 如果此时还是获取不到ip地址，那么最后就使用request.getRemoteAddr()来获取
        * */
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if(StringUtils.pathEquals(ip, LOCAL_IP) || StringUtils.pathEquals(ip,DEFAULT_IP)){
                //根据网卡取本机配置的IP
                InetAddress iNet = null;
                try {
                    iNet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ip= iNet.getHostAddress();
            }
        }
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        //"***.***.***.***".length() = 15
        if(!StringUtils.isEmpty(ip) && ip.length()> DEFAULT_IP_LENGTH){
            if(ip.indexOf(",") > 0){
                ip = ip.substring(0,ip.indexOf(","));
            }
        }
        return ip;
    }
}
